package com.ycb.bookserver.controller.admin;

import com.book.constant.JWTClaimsConstant;
import com.book.properties.JWTProperties;
import com.book.result.Result;
import com.book.utils.JWTUtils;
import com.ycb.book.dto.EmployeeLoginDTO;

import com.ycb.book.entity.Employee;
import com.ycb.book.vo.EmployLoginVO;

import com.ycb.bookserver.service.EmployService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
@RestController
@Slf4j
@RequestMapping("/admin")
public class EmployController {
 @Autowired
 private EmployService employService;
 @Autowired(required = false)
 private JWTProperties jwtProperties;


@PostMapping("/login")
public Result<EmployLoginVO> loginTest(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
    log.info("测试数据登录：{}", employeeLoginDTO);

    Employee employee=employService.login(employeeLoginDTO);

    //登录成功后，生成jwt令牌
    Map<String, Object> claims = new HashMap<>();
    claims.put(JWTClaimsConstant.EMP_ID, employee.getId());
    String token = JWTUtils.createJWT(
            jwtProperties.getAdminSecretKey(),
            jwtProperties.getAdminTtl(),
            claims);

    EmployLoginVO employLoginVO =  EmployLoginVO.builder()
            .id(employee.getId())
            .username(employee.getUsername())
            .token(token)
            .build();

    return Result.success(employLoginVO);
}
}
